# 指定年份，从清芬远程库更新本地数据库
# You should set the following environment variables before running this script
# set sz_username
# set sz_password
# set db_name
# set db_user
# set db_password
import os
import argparse

import MySQLdb

from program import web_api
from program import utility
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--student_id', default=0, type=int, help='if non zero id is given, update single student')
    parser.add_argument('--year', default=2019, type=int)
    parser.add_argument('--ignore_create', default=False, const=True, nargs='?')
    args = parser.parse_args()
    
    session = web_api.login()
    db_user = 'root'
    if(os.environ.get('db_user')):
        db_user = os.environ['db_user']
    db_name = 'student'
    if(os.environ.get('db_name')):
        db_name = os.environ['db_name']
    db_passwd = '123'
    if(os.environ.get('db_password')):
        db_passwd = os.environ['db_password']
    db = MySQLdb.connect(host ="localhost", user=db_user, db=db_name, passwd=db_passwd, charset="utf8")
    cursor = db.cursor()
    if(args.student_id == 0):
        if(args.ignore_create == False):
            dic = {'filter_LIKES_id': args.year}    
            web_api.query_all(session, dic, cursor)
            db.commit()
        student_list = utility.get_student_id_list(cursor, args.year)
        web_api.update_student_info(session, student_list, cursor)
        db.commit()
    else:
        web_api.update_student_info(session, [str(args.student_id)], cursor)
        db.commit()